package main

func leftMax(index int, height []int) int {
	ret := index
	for i := index - 1; i >= 0; i-- {
		if height[i] > height[ret] {
			ret = i
		}
	}

	return ret
}
func rightMax(index int, height []int) int {
	ret := index
	for i := index; i < len(height); i++ {
		if height[i] > height[ret] {
			ret = i
		}
	}

	return ret
}
func trap(height []int) int {
	ans := 0
	for i := 1; i < len(height)-1; i++ {
		left := leftMax(i, height)
		right := rightMax(i, height)

		min := left
		if right < left {
			min = right
		}

		if height[min]-height[i] > 0 {
			ans += min - height[i]
		}

	}

	return ans
}
